#include<iostream>
#include<pthread.h>
#include<unistd.h>
#include"RingBuffer.hpp"


using namespace RingBufferModule;


void* myEQueue(void* arge)
{
   RingBuffer<int>* re=static_cast<RingBuffer<int>*>(arge);
    int lock=0;
    while (true)
    {
        re->EQueue(lock);
        std::cout << "生产了一个数据: " << lock << std::endl;
        lock++;
    }
}

void* mypop(void* arge)
{
    RingBuffer<int>* re=static_cast<RingBuffer<int>*>(arge);
    while (true)
    {
         sleep(1);
        // sleep(1);
        // 1. 消费数据
        int data;
        re->Pop(&data);

        // 2. 处理：花时间
        std::cout << "消费了一个数据: " << data << std::endl;
    }
}

int main()
{
    RingBuffer<int>* r_buff=new RingBuffer<int>(5);
    pthread_t c1,p1,c2,c3,p2;
    pthread_create(&c1,nullptr,mypop,r_buff);
    pthread_create(&c2,nullptr,mypop,r_buff);
    pthread_create(&c3,nullptr,mypop,r_buff);

    pthread_create(&p1,nullptr,myEQueue,r_buff);
    pthread_create(&p2,nullptr,myEQueue,r_buff);



    pthread_join(c1,nullptr);
    pthread_join(c2,nullptr);
    pthread_join(c3,nullptr);

    pthread_join(p1,nullptr);
    pthread_join(p2,nullptr);

    delete r_buff;//当你不自己释放那么会导致数据重复


    return 0;
}